package cxm.proj.sms;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;
import cxm.com.util.MyLog;

public class SendBroadcastReceiver extends BroadcastReceiver {
    private static final String ACTION_SMS_SENT = "SMS_SENT";

    // When the SMS has been sent
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!action.equals(ACTION_SMS_SENT))
        	return;
        
        MyLog.Log(MyLog.Level.Debug, "Receive message 2: " + getResultCode());

        switch (getResultCode()) {
            case Activity.RESULT_OK:
                // Toast.makeText(context, "SMS Sent", Toast.LENGTH_SHORT).show();
                Bundle b = intent.getExtras();
                MyLog.Log(MyLog.Level.Debug, "sendBroadcastReceiver : b is " + b);
                if (b != null) {
                    String value = b.getString("extra_key");
                    MyLog.Log(MyLog.Level.Debug, "sendBroadcastReceiver : value is " + value);
                    
                    // get configuration option
                    Configure.ConfigureOption option = Configure.ConfigureOption.valueOf(value);
                	// set option after send ok
                    Configure.getInstance().setConfig(option, Boolean.toString(
                    		!Configure.getInstance().getConfigBool(option)));
                    break;
                }

                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            case SmsManager.RESULT_ERROR_NO_SERVICE:
            case SmsManager.RESULT_ERROR_NULL_PDU:
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(context, context.getString(R.string.errmsg_sendsms_fail),
                		Toast.LENGTH_SHORT).show();
                break;
        }
// mark send ok;
        SendSMSFragment.isSendSMS = false;
    }
}
